package com.tengju.settlement.infrastructure.repository.convert;

import com.tengju.settlement.domain.model.account.UserIdCode;
import com.tengju.settlement.domain.model.depository.*;
import com.tengju.settlement.domain.shared.IdObject;
import com.tengju.settlement.infrastructure.persistence.mybatis.dataobject.DepositoryAccountDO;

import java.time.LocalDateTime;
import java.util.Optional;

public class DepositoryAccountConvert {

    public static DepositoryAccount convertToModel(DepositoryAccountDO depositoryAccountDO) {
        if (depositoryAccountDO == null) {
            return null;
        }
        return new DepositoryAccount(
                depositoryAccountDO.getId() == null ? null : new DepositoryAccountId(depositoryAccountDO.getId()),
                new UserIdCode(depositoryAccountDO.getIdCode()),
                DepositoryChannel.ofInnerCode(depositoryAccountDO.getDepositoryChannel()),
                new License(LicenseType.fromVal(depositoryAccountDO.getLicenseType()),
                        depositoryAccountDO.getLicenseNumber()),
                depositoryAccountDO.getDepositoryId(),
                AccountStatus.fromVal(depositoryAccountDO.getAccountStatus())
        );
    }


    public static DepositoryAccountDO convertToDO(DepositoryAccount depositoryAccount) {

        if (depositoryAccount == null) {
            return null;
        }

        return new DepositoryAccountDO(
                Optional.ofNullable(depositoryAccount.getDepositoryAccountId()).map(IdObject::getValue).orElse(null),
                depositoryAccount.getIdCode().getValue(),
                depositoryAccount.getDepositoryChannel().getInnerVal(),
                depositoryAccount.getLicense().getLicenseType().getInnerVal(),
                depositoryAccount.getLicense().getLicenseNumber(),
                depositoryAccount.getDepositoryId(),
                depositoryAccount.getStatus().getVal(),
                LocalDateTime.now(),
                LocalDateTime.now()
        );
    }

}
